import { Message } from "element-ui";

// 时间戳：1637244864707
/* 时间戳转换为时间 */
function timestampToTime(timestamp) {
  timestamp = timestamp ? timestamp : null;
  let date = new Date(timestamp); //时间戳为10位需*1000，时间戳为13位的话不需乘1000
  let Y = date.getFullYear() + "-";
  let M =
    (date.getMonth() + 1 < 10
      ? "0" + (date.getMonth() + 1)
      : date.getMonth() + 1) + "-";
  let D = (date.getDate() < 10 ? "0" + date.getDate() : date.getDate()) + " ";
  let h =
    (date.getHours() < 10 ? "0" + date.getHours() : date.getHours()) + ":";
  let m =
    (date.getMinutes() < 10 ? "0" + date.getMinutes() : date.getMinutes()) +
    ":";
  let s = date.getSeconds() < 10 ? "0" + date.getSeconds() : date.getSeconds();
  return Y + M + D + h + m + s;
}

// 北京时间：2021-11-18 22:14:24
/* 时间yyyy-MM-dd HH:mm:ss转为时间戳 */
function timeToTimestamp(time) {
  let timestamp = Date.parse(new Date(time).toString());
  //timestamp = timestamp / 1000; //时间戳为13位需除1000，时间戳为13位的话不需除1000
  //console.log(time + "的时间戳为：" + timestamp);
  return timestamp;
  //2021-11-18 22:14:24的时间戳为：1637244864707
}

export function timeComparison(
  time,
  is,
  oneStartTime,
  oneEndTime,
  twoStartTime,
  twoEndTime
) {
  // "传入开始时间和结束时间,是否大于30天，大于返回false,小于等于返回true"
  // 一天的时间戳 86400000

  const month = 86400000 * time;
  const one = oneStartTime && oneEndTime;
  const two = twoStartTime && twoEndTime;

  if (one && !two) {
    const endTimeOne = timeToTimestamp(oneEndTime);
    const startTimeOne = timeToTimestamp(oneStartTime);
    const timeOne = endTimeOne - startTimeOne;

    if (endTimeOne < startTimeOne) {
      Message({
        message: "开始时间不能大于结束时间！",
        type: "error",
      });
      return false;
    } else {
      if (timeOne <= month) {
        return true;
      } else {
        Message({
          message: `时间段仅支持${time}天内！`,
          type: "error",
        });
        return false;
      }
    }
  } else if (!one && two) {
    const endTimeTwo = timeToTimestamp(twoEndTime);
    const startTimeTwo = timeToTimestamp(twoStartTime);
    const timeTwo = endTimeTwo - startTimeTwo;

    if (endTimeTwo < startTimeTwo) {
      Message({
        message: "开始时间不能大于结束时间！",
        type: "error",
      });
      return false;
    } else {
      if (timeTwo <= month) {
        return true;
      } else {
        Message({
          message: `时间段仅支持${time}天内！`,
          type: "error",
        });
        return false;
      }
    }
  } else if (one && two) {
    const endTimeOne = timeToTimestamp(oneEndTime);
    const startTimeOne = timeToTimestamp(oneStartTime);
    const timeOne = endTimeOne - startTimeOne;

    const endTimeTwo = timeToTimestamp(twoEndTime);
    const startTimeTwo = timeToTimestamp(twoStartTime);
    const timeTwo = endTimeTwo - startTimeTwo;

    const monthOne = timeOne <= month;
    const monthTwo = timeTwo <= month;

    const isOne = endTimeOne < startTimeOne;
    const isTwo = endTimeTwo < startTimeTwo;

    if (isOne && isTwo) {
      Message({
        message: "开始时间不能大于结束时间！",
        type: "error",
      });
      return false;
    } else {
      if (monthOne && monthTwo) {
        return true;
      } else {
        Message({
          message: `时间段仅支持${time}天内！`,
          type: "error",
        });
        return false;
      }
    }
  } else {
    if (is === "earching") {
      return true;
    } else {
      Message({
        message: "请选择正确时间段！",
        type: "error",
      });
      return false;
    }
  }
}
